package com.jic.market.impl;

import java.lang.Long;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.common.base.MarketBaseService;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.common.util.BeanUtil;
import com.jic.market.entity.MaterialCategory;
import com.jic.market.entity.Tag;
import com.jic.market.entity.TagCategory;
import com.jic.market.mapper.MaterialCategoryMapper;
import com.jic.market.mapper.PromotionEntityMapper;
import com.jic.market.mapper.TagCategoryMapper;
import com.jic.market.mapper.TagMapper;
import com.jic.market.service.TagCategoryService;
import com.jic.market.request.*;
import com.jic.market.response.*;


import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 标签分类表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-10 11:41:25
 */
@Slf4j
@Service
public class TagCategoryServiceImpl extends MarketBaseService implements TagCategoryService {

    @Autowired
    private TagCategoryMapper tagCategoryMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private PromotionEntityMapper promotionEntityMapper;

    @Override
    public long insert(TagCategory tagCategory) {
        long flag = tagCategoryMapper.insert(tagCategory);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return tagCategoryMapper.delete(id);
    }

    @Override
    public long update(TagCategory tagCategory) {
        return tagCategoryMapper.update(tagCategory);
    }

    @Override
    public TagCategory queryByPrimaryKey(Long id) {
        return tagCategoryMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<TagCategory> query(TagCategory tagCategory) {
        return tagCategoryMapper.query(tagCategory);
    }


    public RestResult<PageResult<TagCategoryFirstInfo>> queryPage(TagCategory request) {
        return executeQuery(() -> {
            //一级标签分类查询
            List<TagCategoryFirstInfo> tagCategoryList = new ArrayList<>();
            TagCategory firstTag = new TagCategory();
            firstTag.setCategoryLevel(1);
            firstTag.setDeleteFalg(false);
            if (null != request.getStoreId()) {
                firstTag.setStoreId(request.getStoreId());
            }
            if (null != request.getDeptId()) {
                firstTag.setDeptId(request.getDeptId());
            }
            if (null != request.getMerchantId()) {
                firstTag.setMerchantId(request.getMerchantId());
            }
            List<TagCategory> firstTagList = tagCategoryMapper.query(firstTag);
            if (null != firstTagList && !firstTagList.isEmpty()) {
                for (TagCategory tagCategory1 : firstTagList) {
                    TagCategoryFirstInfo firstTagInfo = new TagCategoryFirstInfo();
                    TagCategoryInfo firstTagCategory = new TagCategoryInfo();
                    BeanUtils.copyProperties(tagCategory1, firstTagCategory);
                    firstTagInfo.setTagCategoryInfo(firstTagCategory);
                    //二级分类标签查询
                    List<TagCategorySecondInfo> tagCategoryList2 = new ArrayList<>();
                    TagCategory secondTag = new TagCategory();
                    secondTag.setCategoryLevel(2);
                    secondTag.setCategoryPid(tagCategory1.getId());
                    secondTag.setDeleteFalg(false);
                    if (null != request.getStoreId()) {
                        secondTag.setStoreId(request.getStoreId());
                    }
                    if (null != request.getDeptId()) {
                        secondTag.setDeptId(request.getDeptId());
                    }
                    if (null != request.getMerchantId()) {
                        secondTag.setMerchantId(request.getMerchantId());
                    }
                    List<TagCategory> secondTagList = tagCategoryMapper.query(secondTag);
                    if (null != secondTagList && !secondTagList.isEmpty()) {
                        for (TagCategory tagCategory2 : secondTagList) {
                            TagCategorySecondInfo secondTagCategoryInfo = new TagCategorySecondInfo();
                            TagCategoryInfo secondTagCategory = new TagCategoryInfo();
                            BeanUtils.copyProperties(tagCategory2, secondTagCategory);
                            secondTagCategoryInfo.setTagCategoryInfo(secondTagCategory);
                            //三级分类查询
                            List<TagCategoryThirdInfo> tagCategoryList3 = new ArrayList<>();
                            TagCategory thirdTag = new TagCategory();
                            thirdTag.setCategoryLevel(3);
                            thirdTag.setCategoryPid(tagCategory2.getId());
                            thirdTag.setDeleteFalg(false);
                            if (null != request.getStoreId()) {
                                thirdTag.setStoreId(request.getStoreId());
                            }
                            if (null != request.getDeptId()) {
                                thirdTag.setDeptId(request.getDeptId());
                            }
                            if (null != request.getMerchantId()) {
                                thirdTag.setMerchantId(request.getMerchantId());
                            }
                            List<TagCategory> thirdTagList = tagCategoryMapper.query(thirdTag);
                            if (null != thirdTagList && !thirdTagList.isEmpty()) {
                                for (TagCategory tagCategory3 : thirdTagList) {
                                    TagCategoryThirdInfo thirdTagInfo = new TagCategoryThirdInfo();
                                    TagCategoryInfo thirdTagCategory = new TagCategoryInfo();
                                    BeanUtils.copyProperties(tagCategory3, thirdTagCategory);
                                    thirdTagInfo.setTagCategoryInfo(thirdTagCategory);
                                    tagCategoryList3.add(thirdTagInfo);
                                }
                                secondTagCategoryInfo.setInfoList(tagCategoryList3);
                            }
                            tagCategoryList2.add(secondTagCategoryInfo);
                        }
                        firstTagInfo.setInfoList(tagCategoryList2);
                    }
                    tagCategoryList.add(firstTagInfo);
                }
            }
            PageResult pageResult = new PageResult();
            pageResult.setRows(tagCategoryList);

            return pageResult;
        });
    }

    @Override
    public RestResult<Long> insertTag(TagCategory param) {
        return executeQuery(() -> {
            TagCategory tagCategory = new TagCategory();
            BeanUtils.copyProperties(param, tagCategory, "storeId", "merchantId", "deptId");
            try {
                if (StringUtils.isNullOrEmpty(tagCategory.getSort())) {//排序是否为空
                    tagCategory.setSort("1");
                }
                tagCategory.setDeleteFalg(false);
                if (null != param.getStoreId()) {
                    tagCategory.setStoreId(param.getStoreId());
                }
                if (null != param.getAdminOperate().getDeptId()) {
                    tagCategory.setDeptId(param.getAdminOperate().getDeptId());
                }
                if (null != param.getMerchantId()) {
                    tagCategory.setMerchantId(param.getMerchantId());
                }
                tagCategory.setUpdateTime(new Date());
                tagCategory.setCreatorName(param.getAdminOperate().getOperateName());
                long flag = tagCategoryMapper.insert(tagCategory);
            } catch (Exception e) {
                log.error("名称保存异常，categoryName={}");
                throw new MarketException(MarketErrorCodeEnum.CATEGORY_ERROR, "此分类名已被使用，请重新命名");
            }
            return null;
        });
    }

    @Override
    public RestResult<Long> editTag(TagCategory param) {
        return executeQuery(() -> {
            TagCategory tagCategory = new TagCategory();
            BeanUtils.copyProperties(param, tagCategory);
            // 判断是否被标签表使用
            Tag tag = new Tag();
            tag.setCategoryId(param.getId());
            List<Tag> tags = tagMapper.query(tag);
            if (tags.size() != 0) {
                log.error("分类使用异常，categoryId={}");
                throw new MarketException(MarketErrorCodeEnum.TYPE_ERROR, "已有表关联此分类，无法编辑");
            } else {
                try {
                    if (StringUtils.isNullOrEmpty(tagCategory.getSort())) {//排序是否为空
                        tagCategory.setSort("1");
                    }
                    tagCategory.setUpdateTime(new Date());
                    tagCategory.setUpdatorName(param.getUpdatorName());
                    long flag = tagCategoryMapper.update(tagCategory);
                } catch (Exception e) {
                    log.error("名称保存异常，categoryName={}");
                    throw new MarketException(MarketErrorCodeEnum.CATEGORY_ERROR, "此分类名已被使用，请重新命名");
                }
            }
            return null;
        });
    }

    @Override
    public RestResult<Long> delTag(TagCategory param) {
        return executeQuery(() -> {
            Long flag;
            // 判断是否被标签表使用
            Tag tag = new Tag();
            tag.setCategoryId(param.getId());
            List<Tag> tags = tagMapper.query(tag);

            if (tags.size() != 0) {
                log.error("分类使用异常，categoryId={}");
                throw new MarketException(MarketErrorCodeEnum.TYPE_ERROR, "已有表关联此分类，无法删除");

            } else {
                Long categoryId = param.getId();
                flag = tagCategoryMapper.delete(categoryId);
                return flag;

            }

        });
    }

    //通用查询分类
    @Override
    public RestResult<PageResult<TagCategoryInfo>> getTagList(TagCategory tagCategory) {

        return executeQuery(() -> {
            TagCategory param = new TagCategory();
            BeanUtils.copyProperties(tagCategory, param);
            param.setDeleteFalg(false);
            List<TagCategory> tagList = tagCategoryMapper.queryList(param);
            PageResult result = new PageResult();
            result.setRows(tagList);
            return result;
        });
    }

}